/*
 * Created on Jun 8, 2004
 * by Mihai Postelnicu
 * email mihai@code.ro
 *
 *
 */
package ro.gateway.aida.search;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.Utils;

/**
 * @author Mihai Postelnicu
 * email mihai@code.ro
 */
public class ResultsFormatAction extends MultiTilesAction {


	public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = UserUtils.getLogged(request, response);
		if(user==null) return mapping.findForward("plslogin");
		return  modeSelect(mapping,form,request,response);
	}
	
	public ActionForward modeNew(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			ResultsFormat rf=new ResultsFormat();
			request.setAttribute("resformat",rf);
			return mapping.findForward("edit");
	}

	public ActionForward modeDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			ResultsFormat rf=new ResultsFormat();
			ResultsFormatDB rfdb=ResultsFormatDB.getManager(token);
			String[] delformat=request.getParameterValues("delformat");
			
			for(int i=0;delformat!=null && i<delformat.length; i++) {
				rfdb.delete(Integer.parseInt(delformat[i]));
			}
			return modeList(mapping,form,request,response);
	}

	
	public ActionForward modeEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			long id=Long.parseLong(request.getParameter("id"));
			ResultsFormatDB rfdb=ResultsFormatDB.getManager(token);
			User user = UserUtils.getLogged(request, response);
			ResultsFormat rf=rfdb.get(id);
			request.setAttribute("resformat",rf);
			return mapping.findForward("edit");
	}

	public ActionForward modeSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {			
			ResultsFormatDB rfdb=ResultsFormatDB.getManager(token);
			long id=Long.parseLong((String)request.getParameter("id"));	
			User user = UserUtils.getLogged(request, response);
			ResultsFormat rf=new ResultsFormat();
			rf.setUserId(user.getId());
			rf.setName(request.getParameter("name"));
			String[] picked=request.getParameterValues("PickList");
			for(int i=0;i<picked.length;i++) {
				rf.addColumn(picked[i]);
			}
			
			rfdb.insert(rf);
			return modeList(mapping,form,request,response);	
	}

	public ActionForward modeUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {			
			long id=Long.parseLong(request.getParameter("id"));	
			ResultsFormatDB rfdb=ResultsFormatDB.getManager(token);
			User user = UserUtils.getLogged(request, response);
			ResultsFormat rf=new ResultsFormat();
			rf.setUserId(user.getId());
			rf.setId(id);
			rf.setName(request.getParameter("name"));
			String[] picked=request.getParameterValues("PickList");
			for(int i=0;i<picked.length;i++) {
				rf.addColumn(picked[i]);
			}
			rfdb.update(rf);
			return modeList(mapping,form,request,response);	
	}

	
	
	public ActionForward modeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {			
			ResultsFormatDB rfdb=ResultsFormatDB.getManager(token);
			User user = UserUtils.getLogged(request, response);
			List l=rfdb.getForUser(user.getId(),false);
			request.setAttribute("formats",l);
			Utils.setBreadCrumbs(request, "bc.prjsearch.format.list");
			return mapping.findForward("list");
	}
	
	public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			String mode=request.getParameter("mode");
			Utils.setBreadCrumbs(request, "bc.prjsearch.format.edit");
			if("edit".equals(mode)) return modeEdit(mapping,form,request,response);
			if("list".equals(mode)) return modeList(mapping,form,request,response);
			if("save".equals(mode)) return modeSave(mapping,form,request,response);			
			if("update".equals(mode)) return modeUpdate(mapping,form,request,response);			
			if("delete".equals(mode)) return modeDelete(mapping,form,request,response);
			return modeNew(mapping,form,request,response);
	}

}
